package CCRMClient.GUI;
/**
 * This screen is for assigning new customers for the sale that was initiated.
 * I assumed that the task force is no more than 7 salesmen. if more needed, than we can add.
 * @author Oz Mouchtar
 */
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLayeredPane;
import javax.swing.JTable;
import javax.swing.ListModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.CardLayout;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import java.awt.BorderLayout;
import java.io.IOException;

import javax.swing.ListSelectionModel;

import CCRMClient.ClientStart;
import Common.SERVER_COMMANDS;
import Common.Entities.SalesmenAndCustomersForSale;

import java.awt.Color;

public class ChooseTaskForce extends JPanel  {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JScrollPane scrollPane;
	private JLabel lblChooseSalesTaskforce;
	private JButton btnAssigneCustomers;
    private SalesmenAndCustomersForSale info;
	private JTable tblsalesmen;
	private JButton btnAbort;

	public ChooseTaskForce(SalesmenAndCustomersForSale data) {
		this.info = data;
		initComponents();
		

		
	}
	private void initComponents() {
		setBackground(Color.WHITE);
		setLayout(null);
		add(getScp());
		loadSalesmen();
		
		lblChooseSalesTaskforce = new JLabel("Choose sale's Task-Force ");
		lblChooseSalesTaskforce.setForeground(Color.RED);
		lblChooseSalesTaskforce.setHorizontalAlignment(SwingConstants.CENTER);
		lblChooseSalesTaskforce.setFont(new Font("David", Font.BOLD, 24));
		lblChooseSalesTaskforce.setBounds(85, 20, 298, 25);
		add(lblChooseSalesTaskforce);
		
		btnAssigneCustomers = new JButton("Assigne customers");
		btnAssigneCustomers.addActionListener(new BtnAssigneCustomersActionListener());
		btnAssigneCustomers.setFont(new Font("Tahoma", Font.BOLD, 16));
		btnAssigneCustomers.setBounds(227, 266, 205, 23);
		add(btnAssigneCustomers);
		

		
		btnAbort = new JButton("Abort");
		btnAbort.addActionListener(new BtnAbortActionListener());
		btnAbort.setFont(new Font("Tahoma", Font.BOLD, 16));
		btnAbort.setBounds(12, 266, 205, 23);
		add(btnAbort);
	}
	private void loadSalesmen() {
		if (!info.getSalesmen().isEmpty()) {
			DefaultTableModel dtm = (DefaultTableModel) tblsalesmen.getModel();
			dtm.removeRow(0);
			for (String[] row : info.getSalesmen())
				dtm.addRow(row);
		}
	}
		JScrollPane getScp() {
			if (scrollPane == null) {
				scrollPane = new JScrollPane();
				scrollPane.setBounds(20, 49, 412, 187);
				scrollPane.setViewportView(getTbSalesmen());
			}
			return scrollPane;
	}
		JTable getTbSalesmen() {
			if (tblsalesmen == null) {
				tblsalesmen = new JTable();
				tblsalesmen.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				tblsalesmen.setModel(new DefaultTableModel(
					new Object[][] {
						{null, null},
					},
					new String[] {
						"Salesman ID", "Salesman Name"
					}
				) {
					Class[] columnTypes = new Class[] {
						String.class, String.class
					};
					public Class getColumnClass(int columnIndex) {
						return columnTypes[columnIndex];
					}
				});
				tblsalesmen.setFont(new Font("David", Font.BOLD, 11));
			}
			return tblsalesmen;
		}


	private class BtnAssigneCustomersActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			int selected = tblsalesmen.getSelectedRow();
			if (selected == -1) 
				JOptionPane.showMessageDialog(null, "Please select a salesman", "Assigne Task-Force", JOptionPane.INFORMATION_MESSAGE);
			DefaultTableModel dtm = (DefaultTableModel) tblsalesmen.getModel();
			String[] salesman = new String[2];
			salesman[0] = (String) dtm.getValueAt(selected, 0);
			salesman[1] = (String) dtm.getValueAt(selected, 1);
			info.setSalesmanID(salesman[0]);
			info.setSalesmanName(salesman[1]);
			info.deleteFromSalesmenList(salesman);
			ClientStart.panelSwicher(new AssigneCustomersScreen(info));
			
			
		}
	}
	private class BtnAbortActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(null, "Sale has not been saved", "Assigne Task-Force", JOptionPane.INFORMATION_MESSAGE);
			ClientStart.panelSwicher(new MarketingManagementScreen());
		}
	}
}
